//
//  ShoucahngTool.m
//  App-MyFairldy
//
//  Created by 客官大大 on 15/5/26.
//  Copyright (c) 2015年 客官大大. All rights reserved.
//

#import "ShoucahngTool.h"
static sqlite3 *dbPoint = nil;
@implementation ShoucahngTool
//-(instancetype)init
//{
//    self = [super init];
//    if (self) {
//        [self creatdata];
//    }
//    return self;
//}
+(void)openDatabse
{
    if (dbPoint) {
        return;
    }
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    path = [path stringByAppendingPathComponent:@"Food.rdb"];
    sqlite3_open(path.UTF8String, &dbPoint);
}
+(void)createTable
{
    [self openDatabse];

    NSString *sql = @"create table shoucang(name varchar(20),calory varchar(20), appraise varchar(200))";
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+(void)insertValue:(ShoucangModel *)shou
{
    [self openDatabse];
    NSString *sql = [NSString stringWithFormat:@"insert into shoucang values ('%@', '%@', '%@')",shou.name, shou.calory, shou.appraise];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+(NSArray *)selectData
{
    [self openDatabse];
          NSMutableArray *arry = [NSMutableArray array];
    NSString *sql = [NSString stringWithFormat:@"select * from shoucang"];
    
//    sqlite3 *db = dbPoint;
    sqlite3_stmt *stmt;
    int result = sqlite3_prepare_v2(dbPoint, [sql UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
//            const unsigned char *myId = sqlite3_column_text(stmt, 1);
            const unsigned char *name = sqlite3_column_text(stmt, 0);
//            const unsigned char *nicname = sqlite3_column_text(stmt, 3);
//            const unsigned char *address = sqlite3_column_text(stmt, 4);
            
            ShoucangModel *model = [[ShoucangModel alloc] init];
            model.name = [NSString stringWithUTF8String:(const char *)name];
      
            [arry addObject:model];
            
            
            
            
        }
    }
    
    return arry;

}


@end
